package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 完成用户注册功能
 * 要求:
 * 程序启动后要求用户在控制台上陆续输入:用户名，密码，昵称，年龄 四项
 * 将用户信息插入到user表中完成注册
 */
public class RegDemo {
    public static void main(String[] args) throws Exception {
//        System.out.println("欢迎注册");
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入用户名:");
//        String username = scanner.nextLine();
//        System.out.println("请输入密码:");
//        String password = scanner.nextLine();
//        System.out.println("请输入昵称:");
//        String nickname = scanner.nextLine();
//        System.out.println("请输入年龄:");
//        int age = scanner.nextInt();
//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setNickname(nickname);
//        user.setAge(age);

        User user = InputUtil.getInputObject(new User(),"欢迎注册");
        System.out.println(user);
        insert(user);
    }

    public static void insert(User user) throws Exception {
        String sql = "INSERT INTO user(username,password,nickname,age) " +
                "VALUES('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"',"+user.getAge()+")";
        System.out.println(sql);
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        Statement statement = connection.createStatement();
        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "注册成功" : "注册失败");
        connection.close();
    }
}








